﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using clsEmployeeList.Forms;
using libMasterSetup.Forms.Billing;
using libMasterSetup.Forms.SalesTermsAndTrade;
using libMasterSetup.Forms;
using libQoutation.Forms;
using libDAL;
using libCommonTools.Forms;
using Telerik.WinControls.Themes.Design;
using Telerik.WinControls;
using libAddressBook.FORMS;
using libInventory.FORMS;

namespace libLogin.Forms
{
    public partial class MDIParent1 : Form
    {
        private struct ModulesForms
        {
            public frmFixOptionalCosting frmCosting;
            public frmSalesAndTrade frmSalesAndTrade;
            public frmChargesSetup frmChargesetup;
            public frmGRN frmGRN;
            public frmLCLQoutation frmLCLQuotation;
            public frmUserProfile frmUserProfile;
            public frmAddressBook frmAddressBook;
            public frmInlandOriginDestination frmInlandCharges;
            public frmODChargesCustomizer frmChargesCustomizer;
           
          };

        private string strCompanyID = string.Empty;
        private string strCompanyType = string.Empty;

        private string getset_CompanyType
        {
            get
            {
                return strCompanyType;
            }
            set
            {
                strCompanyType = value;
            }
        }

        private string getset_CompanyID
        {
            get
            {
                return strCompanyID;
            }
            set
            {
                strCompanyID = value;
            }
        }

        private struct Communication
        {
                  public cls_DAL _DAL;
        }

        private ModulesForms frmModule;
        private Communication genCommunication;

        private int childFormNumber = 0;

        public MDIParent1()
        {
            InitializeComponent();
         //   ThemeResolutionService.ApplicationThemeName = "Red";
         //   genCommunication._DAL = new cls_DAL(genCommunication.strTempConStr);
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void radButtonElement17_Click(object sender, EventArgs e)
        {

            // rrbMedia.Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            rrbMedia.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
            rrbAddressBookBranchOperation.Visibility = Telerik.WinControls.ElementVisibility.Visible;
        }

        private void radButtonElement15_Click(object sender, EventArgs e)
        {

        }

        private void rbeCosting_Click(object sender, EventArgs e)
        {
            try
            {
                frmModule.frmChargesCustomizer  = new frmODChargesCustomizer();
                frmModule.frmChargesCustomizer.MdiParent = this;
                frmModule.frmChargesCustomizer.FormBorderStyle = FormBorderStyle.None;
                frmModule.frmChargesCustomizer.ControlBox = false;
                frmModule.frmChargesCustomizer.MinimizeBox = false;
                frmModule.frmChargesCustomizer.MaximizeBox = false;
                frmModule.frmChargesCustomizer.Show();
                frmModule.frmChargesCustomizer.Dock = DockStyle.Fill;
                rbtControls.IsSelected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeCosting_Click");
            }
        }

        private void MDIParent1_Load(object sender, EventArgs e)
        {
            this.IsMdiContainer = true;
            this.ShowIcon = true;
            this.dckMainTab.AutoDetectMdiChildren = true;
            dckMainTab.DockWindowAdded += new Telerik.WinControls.UI.Docking.DockWindowEventHandler(radDock1_DockWindowAdded);
            set_Collapse(string.Empty);
            

        }

        private void radDock1_DockWindowAdded(object sender, Telerik.WinControls.UI.Docking.DockWindowEventArgs e)
        {
            dckMainTab.ActiveWindow = e.DockWindow;
        }

        private void rbeSalesTerm_Click(object sender, EventArgs e)
        {
           frmModule.frmSalesAndTrade = new frmSalesAndTrade();
           frmModule.frmSalesAndTrade.MdiParent = this;
           frmModule.frmSalesAndTrade.FormBorderStyle = FormBorderStyle.None;
           frmModule.frmSalesAndTrade.ControlBox = false;
           frmModule.frmSalesAndTrade.MinimizeBox = false;
           frmModule.frmSalesAndTrade.MaximizeBox = false;
           frmModule.frmSalesAndTrade.Show();
           frmModule.frmSalesAndTrade.Dock = DockStyle.Fill;
           rbtControls.IsSelected = true;
        }

        private void radDock1_ActiveWindowChanged(object sender, Telerik.WinControls.UI.Docking.DockWindowEventArgs e)
        {
                set_Collapse(e.DockWindow.Name);
        }

        private void set_Collapse(string strName)
        {
            try
            {
                int intxt = rbtControls.Items.Count;
                for (int i = 2; i < intxt; i++)
                {
                    if (strName.Trim() != string.Empty)
                    {
                        if (rbtControls.Items[i].Name.Contains(strName.Substring(3, strName.Length - 4)) == false)
                        {
                            rbtControls.Items[i].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                        }
                        else
                        {
                            rbtControls.Items[i].Visibility = Telerik.WinControls.ElementVisibility.Visible;
                        }
                    }
                    else
                    {
                        rbtControls.Items[i].Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_Collapse"); 
            }
        }

        private void rbePerentage_Click(object sender, EventArgs e)
        {
            frmChargeLevel x = new frmChargeLevel();
            x.ShowDialog();
        }

        private void radButtonElement21_Click(object sender, EventArgs e)
        {

            frmModule.frmChargesetup = new frmChargesSetup();
            frmModule.frmChargesetup.MdiParent = this;
            frmModule.frmChargesetup.FormBorderStyle = FormBorderStyle.None;
            frmModule.frmChargesetup.ControlBox = false;
            frmModule.frmChargesetup.MinimizeBox = false;
            frmModule.frmChargesetup.MaximizeBox = false;
            frmModule.frmChargesetup.Show();
            frmModule.frmChargesetup.Dock = DockStyle.Fill;
            rbtControls.IsSelected = true;

        }

        private void rbeGRN_Click(object sender, EventArgs e)
        {
            frmModule.frmGRN = new frmGRN();
            frmModule.frmGRN.set_BarGroup(ref rgbGRN); 
            frmModule.frmGRN.MdiParent = this;
            frmModule.frmGRN.FormBorderStyle = FormBorderStyle.None;
            frmModule.frmGRN.ControlBox = false;
            frmModule.frmGRN.MinimizeBox = false;
            frmModule.frmGRN.MaximizeBox = false;
            frmModule.frmGRN.Show();
            frmModule.frmGRN.Dock = DockStyle.Fill;
            rbtControls.IsSelected = true;
        }

        private void radButtonElement26_Click(object sender, EventArgs e)
        {
            frmModule.frmLCLQuotation = new frmLCLQoutation();
            frmModule.frmLCLQuotation.MdiParent = this;
            frmModule.frmLCLQuotation.ControlBox = false;
            frmModule.frmLCLQuotation.MinimizeBox = false;
            frmModule.frmLCLQuotation.MaximizeBox = false;
            frmModule.frmLCLQuotation.Show();
            frmModule.frmLCLQuotation.Dock = DockStyle.Fill;
            rbtControls.IsSelected = true;
        }

        private void rbeUsers_Click(object sender, EventArgs e)
        {
            try
            {
                frmModule.frmUserProfile = new frmUserProfile();
                frmModule.frmUserProfile.MdiParent = this;
                frmModule.frmUserProfile.FormBorderStyle = FormBorderStyle.None;
                frmModule.frmUserProfile.ControlBox = false;
                frmModule.frmUserProfile.MinimizeBox = false;
                frmModule.frmUserProfile.MaximizeBox = false;
                frmModule.frmUserProfile.Show();
                frmModule.frmUserProfile.Dock = DockStyle.Fill;
                rbtControls.IsSelected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " miFixOptionalCharges_Click");
            }
        }

        private void rbeAddressBook_Click(object sender, EventArgs e)
        {
            try
            {
                frmModule.frmAddressBook = new frmAddressBook();
                frmModule.frmAddressBook.MdiParent = this;
                frmModule.frmAddressBook.FormBorderStyle = FormBorderStyle.None;
                frmModule.frmAddressBook.ControlBox = false;
                frmModule.frmAddressBook.MinimizeBox = false;
                frmModule.frmAddressBook.MaximizeBox = false;
                frmModule.frmAddressBook.Show();
                frmModule.frmAddressBook.Dock = DockStyle.Fill;
                rbtControls.IsSelected = true;
                //frmAddressBuk xxx = new frmAddressBuk();
                //xxx.Show();
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " miFixOptionalCharges_Click");
            }
        }

        private void radButtonElement19_Click(object sender, EventArgs e)
        {
            try
            {
                if (frmModule.frmAddressBook.getset_SelectedRowStatus == true)
                {
                    frmOperationalSettings xx = new frmOperationalSettings(ref frmModule.frmAddressBook);
                    xx.ShowDialog();
                }
                else
                {
                    frmModule.frmAddressBook.getset_SelectedRowStatus = false;
                    MessageBox.Show("Please select specific Company, try again."); 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " radButtonElement19_Click"); 
            }
        }

        private void radButtonElement18_Click(object sender, EventArgs e)
        {
            frmAddressBookBranch frmBranch = new frmAddressBookBranch();
            frmBranch.ShowDialog(this);
        }

        private void MDIParent1_FormClosed(object sender, FormClosedEventArgs e)
        {

            this.Dispose();
            GC.Collect();
            GC.WaitForFullGCComplete();
            GC.Collect();
        }

        private void rbeInLand_Click(object sender, EventArgs e)
        {
            frmModule.frmInlandCharges = new frmInlandOriginDestination();
            frmModule.frmInlandCharges.MdiParent = this;
            frmModule.frmInlandCharges.FormBorderStyle = FormBorderStyle.None;
            frmModule.frmInlandCharges.ControlBox = false;
            frmModule.frmInlandCharges.MinimizeBox = false;
            frmModule.frmInlandCharges.MaximizeBox = false;
            frmModule.frmInlandCharges.Show();
            frmModule.frmInlandCharges.Dock = DockStyle.Fill;
            rbtControls.IsSelected = true;
        }

        private void rbeMap_Click(object sender, EventArgs e)
        {
            //frmModule.frmMap  = new frmMap();
            //frmModule.frmMap.MdiParent = this;
            //frmModule.frmMap.FormBorderStyle = FormBorderStyle.None;
            //frmModule.frmMap.ControlBox = false;
            //frmModule.frmMap.MinimizeBox = false;
            //frmModule.frmMap.MaximizeBox = false;
            //frmModule.frmMap.Show();
            //frmModule.frmMap.Dock = DockStyle.Fill;

        }

        private void rbeMapDistance_Click(object sender, EventArgs e)
        {
       
        }
        
        private void set_findCompany()
        {
            try
            {
                using (frmFindCompany addressBook = new frmFindCompany("Company"))
                {
                    DialogResult dlResult = addressBook.ShowDialog();
                    if (dlResult == DialogResult.OK)
                    {
                        if (addressBook.get_CompanyIDHQ().Trim() != string.Empty)
                        {
                            frmModule.frmAddressBook.set_RefreshAll();

                            getset_CompanyID = addressBook.get_CompanyIDHQ().Trim();
                            getset_CompanyType = addressBook.get_CompanyType.Trim();

                            frmModule.frmAddressBook.getset_CompanyID = getset_CompanyID;
                            frmModule.frmAddressBook.getset_CompanyType = getset_CompanyType;

                            frmModule.frmAddressBook.set_AddressbookFields();

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_findCompany"); 
            }
        }

        private void set_findGRNInventory()
        {
            try
            {
                using (frmFindCompany addressBookGRN = new frmFindCompany("GRN"))
                {
                    DialogResult dlResult = addressBookGRN.ShowDialog();
                    if (dlResult == DialogResult.OK)
                    {
                        if (addressBookGRN.getset_GRNNumber.Trim() != string.Empty)
                        {
                            this.frmModule.frmGRN.getset_New = false;
                            this.frmModule.frmGRN.getset_Update = true;
                            this.frmModule.frmGRN.set_setGRNDetails(addressBookGRN.getset_GRNNumber.Trim());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_findGRNInventory"); 
            }
        }

        private void set_findChargesSalesTerm()
        {
            try
            {
                using (frmFindSalesTermHeader frmFindSalesTerms = new frmFindSalesTermHeader())
                {
                    DialogResult = frmFindSalesTerms.ShowDialog();
                    if (DialogResult == DialogResult.OK)
                    {
                        if (frmModule.frmChargesCustomizer.set_LoadExistingRecords(frmFindSalesTerms.get_HeaderID) == true)
                        {
                            
                              rbeNewAdd.Enabled = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_findChargesSalesTerm"); 
            }
        }


        private void rbeFind_Click(object sender, EventArgs e)
        {
            try
            {
                Form activeChild = this.ActiveMdiChild;
                if (dckMainTab.ActiveWindow.Name.Trim().ToLower().Contains("address") == true)
                {
                    set_findCompany();
                }
                else if (dckMainTab.ActiveWindow.Name.Trim().ToLower().Contains("grn") == true)
                {
                    set_findGRNInventory();
                }
                else if (dckMainTab.ActiveWindow.Name.Trim().ToLower().Contains("chargescustomizer") == true)
                {
                    set_findChargesSalesTerm();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeFind_Click"); 
            }
        }

        private void rbeRefresh_Click(object sender, EventArgs e)
        {
            if (dckMainTab.ActiveWindow.Name.Trim().Contains("Address") == true)
            {
                frmModule.frmAddressBook.set_RefreshAll();
            }
        }

        private void rbeSave_Click(object sender, EventArgs e)
        {
            try
            {
               
                if (get_ActiveTab().Contains("Address") == true)
                {
                    frmModule.frmAddressBook.set_SaveRecords();
                }
                else if (get_ActiveTab().ToLower().Contains("grn") == true)
                {
                    if (frmModule.frmGRN.set_GenerateSetGRN() == true)
                    {
                        MessageBox.Show("Saved."); 
                    }
                }
                else if (get_ActiveTab().ToLower().Contains("chargescustomizer") == true)
                {
                    
                    if (frmModule.frmChargesCustomizer.set_SaveHeaderRecords() == true)
                    {
                        frmModule.frmChargesCustomizer.set_SaveDetailsRecords();
                    }
                }
                else if (get_ActiveTab().ToLower().Contains("inland") == true)
                {
                    frmModule.frmInlandCharges.set_SaveInlandSettings();  
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeSave_Click"); 
            }
        }

        private void rbeNewAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (get_ActiveTab().Contains("Address") == true)
                {
                    
                    frmModule.frmAddressBook.set_AddNewCompany();
                }
                else if (get_ActiveTab().ToLower().Contains("grn") == true)
                {
                    frmModule.frmGRN.getset_New = true;
                    frmModule.frmGRN.getset_Update = false;
                    frmModule.frmGRN.set_NewGRNFields();
                }
                else if (get_ActiveTab().ToLower().Contains("chargescustomizer") == true)
                {
                    frmModule.frmChargesCustomizer.set_NewRecords(string.Empty);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeNewAdd_Click");  
            }
        }

        private string get_ActiveTab()
        {
            return dckMainTab.ActiveWindow.Name.Trim();
        }

        private void rbeEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (get_ActiveTab().ToLower().Contains("address") == true)
                {
                    frmModule.frmAddressBook.getset_CommandType("Edit");
                    rbeSave.Enabled = true; 
                }
                else if (get_ActiveTab().ToLower().Contains("chargescustomizer") == true)
                {
                    frmModule.frmChargesCustomizer.set_Edit();
                    rbeSave.Enabled = true; 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeEdit_Click"); 
            }
        }

        private void radButtonElement13_Click(object sender, EventArgs e)
        {
            if (get_ActiveTab().Contains("Address") == true)
            {
              //  frmModule.frmAddressBook.getset_CommandType("Cancel");
                frmModule.frmAddressBook.set_Refresh();
            }
        }

        private void rbeAddressBookOperational_Click(object sender, EventArgs e)
        {
            try
            {
                if (frmModule.frmAddressBook.getset_SelectedRowStatus == true)
                {
                    frmOperationalSettings xx = new frmOperationalSettings(ref frmModule.frmAddressBook);
                    xx.ShowDialog();
                }
                else
                {
                    frmModule.frmAddressBook.getset_SelectedRowStatus = false;
                    MessageBox.Show("Please select specific Company, try again.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeAddressBookOperational_Click");
            }
        }

        private void rbeGRNAdditionalServices_Click(object sender, EventArgs e)
        {
            try
            {
                frmModule.frmGRN.set_FindAdditionalServices();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeGRNAdditionalServices_Click"); 
            }
        }

        private void rbeGRNHistory_Click(object sender, EventArgs e)
        {
            try
            {
                frmModule.frmGRN.set_DisplayPreviousTransactions();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " rbeGRNHistory_Click"); 
            }
        }

      

      
    }
}
